package com.woniuxy.qos;

import com.rabbitmq.client.*;
import com.woniuxy.utils.Utils;
import netscape.javascript.JSUtil;

import java.io.IOException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class QosConsume {
    public static void main(String[] args) throws IOException, TimeoutException {
        Connection connection = Utils.getConnection();
        Channel channel = connection.createChannel();
        channel.exchangeDeclare("QosExchange", BuiltinExchangeType.DIRECT);
        channel.queueDeclare("Qosqueue",false,false,false,null);
        //设置限流
        channel.basicQos(20);
        //交换机绑定队列
        channel.queueBind("Qosqueue","QosExchange","qos1");
            channel.basicConsume("Qosqueue",false,new DefaultConsumer(channel){
                @Override
                public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                    System.out.println("QosConsume 接受消息"+new String(body));
                    try {
                        TimeUnit.MILLISECONDS.sleep(100);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    channel.basicAck(envelope.getDeliveryTag(),false);
                }
            });

    }
}
